package day13.thread02;

import java.util.concurrent.locks.ReentrantLock;

/**
 * @author Xiao
 * @date 2021/8/5
 */
public class Ticket4 implements Runnable {
    private static int ticketCount = 100;
    private static Object o = new Object();
    private static ReentrantLock lock = new ReentrantLock();
    @Override
    public void run() {
        while (true) {
            lock.lock();
            try {
                if (ticketCount <= 0) {
                    break;
                }
                Thread.sleep(100);
                System.out.println(Thread.currentThread().getName() +
                        "卖了1张票，还剩下" + --ticketCount + "张");
            } catch (InterruptedException e) {
                e.printStackTrace();
            } finally {
                lock.unlock();
            }
        }
    }
}
